package com.example.cm_player

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.cm_player.databinding.ItemBottomDialogBinding
import com.example.cm_player.model.PlayListSong
import com.example.cm_player.model.SongInfo

class PlayListAdapter(val items: MutableList<PlayListSong>) :
    RecyclerView.Adapter<PlayListAdapter.PlayListViewHolder>() {
    private var lastPlayListSongIndex = -1
    var changeMusicCallback: ((SongInfo) -> Unit)? = null
    var removeMusicCallback: ((SongInfo) -> Unit)? = null

    fun setData(items: List<PlayListSong>) {
        if (this.items.size != 0) {
            this.items.clear()
        }
        this.items.addAll(items)
        notifyDataSetChanged()
    }

    fun removeData(position: Int) {
        if (items.contains(items[position])) {
            if (removeMusicCallback != null) {
                removeMusicCallback!!.invoke(items[position].songInfo)
            }
            this.items.remove(items[position])
            notifyItemRemoved(position)
        }
    }

    fun addItem(position: Int, playListSong: PlayListSong) {
        if (!items.contains(playListSong)) {
            items.add(position, playListSong)
            notifyItemInserted(position)
        }
    }

    fun addItem(playListSong: PlayListSong) {
        if (!items.contains(playListSong)) {
            items.add(playListSong)
            notifyItemInserted(items.size - 1)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayListViewHolder {
        val binding =
            ItemBottomDialogBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PlayListViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return items.size
    }

    override fun onBindViewHolder(holder: PlayListViewHolder, position: Int) {
        holder.bind(position)
    }

    inner class PlayListViewHolder(val binding: ItemBottomDialogBinding) :
        RecyclerView.ViewHolder(binding.root) {

        @SuppressLint("SetTextI18n")
        fun bind(position: Int) {
            val playListSong = items[position]
            if (playListSong.isShow) {
                if (lastPlayListSongIndex == -1) {
                    lastPlayListSongIndex = position
                }
                binding.cmSongWaveView.visibility = View.VISIBLE
                binding.root.setBackgroundColor(Color.parseColor("#262628"))
                binding.cmBottomDialogArtistAndTitle.setTextColor(Color.RED)
                binding.cmSongWaveView.startAnimator()
            } else {
                binding.cmSongWaveView.visibility = View.GONE
                binding.root.setBackgroundColor(Color.parseColor("#2C2C2C"))
                binding.cmBottomDialogArtistAndTitle.setTextColor(Color.WHITE)
                binding.cmSongWaveView.cancelAnimator()
            }
            binding.cmBottomDialogArtistAndTitle.text =
                "${playListSong.songInfo.title}·${playListSong.songInfo.artist}"

            binding.cmDelete.setOnClickListener {
                if (lastPlayListSongIndex == adapterPosition) {
                    binding.cmSongWaveView.cancelAnimator()
                    lastPlayListSongIndex = -1
                } else if (lastPlayListSongIndex > adapterPosition) {
                    lastPlayListSongIndex -= 1
                }
                removeData(adapterPosition)

            }

            binding.root.setOnClickListener {
                if (lastPlayListSongIndex != adapterPosition) {
                    if (lastPlayListSongIndex != -1) {
                        items[lastPlayListSongIndex].isShow = false
                        notifyItemChanged(lastPlayListSongIndex)
                    }
                    if (changeMusicCallback != null) {
                        changeMusicCallback!!.invoke(items[adapterPosition].songInfo)
                    }
                    binding.cmSongWaveView.visibility = View.VISIBLE
                    binding.root.setBackgroundColor(Color.parseColor("#262628"))
                    binding.cmBottomDialogArtistAndTitle.setTextColor(Color.RED)
                    playListSong.isShow = true
                    binding.cmSongWaveView.startAnimator()
                    lastPlayListSongIndex = adapterPosition
                }
            }
        }
    }

}